package com.bnlr.lrparking.appui.stall;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bnlr.lrparking.appui.search.SearchStallActivity;
import com.bnlr.lrparking.appui.login.VerificaActivity;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.adapter.HorizontalAdapter;
import com.bnlr.lrparking.adapter.SearchStallAdapter;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.model.StallData;
import com.bnlr.lrparking.model.StallM;
import com.bnlr.lrparking.constant.Params;
import com.bnlr.lrparking.util.CommonUtil;
import com.bnlr.lrparking.view.CarCommon;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.shizhefei.view.indicator.Indicator;
import com.shizhefei.view.indicator.ScrollIndicatorView;
import com.shizhefei.view.indicator.slidebar.ColorBar;
import com.shizhefei.view.indicator.transition.OnTransitionTextListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author tian
 * @date 2017/10/24
 * 利人停车（虚拟车位）
 */

public class StallActivity extends BaseActivity{
    private HorizontalAdapter horizontalAdapter;
    private ScrollIndicatorView scrollIndicatorView;
    private List<String> list = new ArrayList<>();
    private RelativeLayout synth_sort, price_sort, earnings_sort, stall_search, no_data;
    private TextView tv_synth, tv_price, tv_earnings;//
    List<StallData> stallList = new ArrayList<>();
    private RecyclerView lvSearchList;
    private SearchStallAdapter stallAdapter;
    private int ownerType = 0;//车位所属
    private int orderBy = 0;//车位排序
    private int currentPage = 1;
    private String keyWords = "";
    private RelativeLayout back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_stall);
        initView();
    }
    private void initView() {
        scrollIndicatorView = (ScrollIndicatorView) findViewById(R.id.complain_scrollIndicatorView);
        list.add("平台车位");
        list.add("个人车位");
        list.add("开发商车位");
        list.add("其他车位");
        list.add("收藏车位");
        set(scrollIndicatorView, list);
        synth_sort = (RelativeLayout) findViewById(R.id.synth_sort);
        price_sort = (RelativeLayout) findViewById(R.id.price_sort);
        back = (RelativeLayout) findViewById(R.id.rely_dw);
        earnings_sort = (RelativeLayout) findViewById(R.id.earnings_sort);
        stall_search = (RelativeLayout) findViewById(R.id.stall_search);
        no_data = (RelativeLayout) findViewById(R.id.no_data);
        tv_synth = (TextView) findViewById(R.id.tv_synth);
        tv_price = (TextView) findViewById(R.id.tv_price);
        tv_earnings = (TextView) findViewById(R.id.tv_earnings);
        lvSearchList = (RecyclerView) findViewById(R.id.lv_stall_list);
        lvSearchList.setLayoutManager(new LinearLayoutManager(this));
        stallAdapter =new SearchStallAdapter(stallList);
        stallAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                //  上拉加载
                currentPage++;
                getListData(ownerType, orderBy,currentPage,keyWords,"load");
            }
        },lvSearchList);
        lvSearchList.setAdapter(stallAdapter);
        tv_synth.setTextColor(this.getResources().getColor(R.color.main_color));
        synth_sort.setOnClickListener(this);
        price_sort.setOnClickListener(this);
        earnings_sort.setOnClickListener(this);
        stall_search.setOnClickListener(this);
        stallAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int i) {

                Intent intent = new Intent(StallActivity.this, StallDetailsActivity.class);
                intent.putExtra("monthCount",stallList.get(i).getMonthCount());//最大投资时间
                intent.putExtra("id", stallList.get(i).getId());
                intent.putExtra("number", stallList.get(i).getNumber());
                intent.putExtra("type", stallList.get(i).getParkSpaceType());//类型
                intent.putExtra("size", stallList.get(i).getArea());//大小
                intent.putExtra("their", stallList.get(i).getOwnerType());
                intent.putExtra("incomerockon", stallList.get(i).getIncomeRockon());//收益值
                intent.putExtra("sellingprice", stallList.get(i).getSellingPrice());//共享值
                intent.putExtra("time", stallList.get(i).getBeginTime());
                intent.putExtra("time1", stallList.get(i).getEndTime());
                intent.putExtra("address", stallList.get(i).getAddress());
                intent.putExtra("parkphoto",stallList.get(i).getParkPhoto());
                intent.putExtra("IsFavorite",stallList.get(i).getIsFavorite());
                startActivity(intent);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        currentPage = 1;
        getListData(ownerType, orderBy,currentPage,keyWords,"refresh");
    }

    private void set(final Indicator indicator, List list) {
        int selectColor = getResources().getColor(R.color.main_color);
        int unSelectColor = getResources().getColor(R.color.text_color);
        indicator.setAdapter(horizontalAdapter = new HorizontalAdapter(this, list));
        indicator.setScrollBar(new ColorBar(this.getApplicationContext(), selectColor, 6));
        float unSelectSize = 13;
        float selectSize = unSelectSize * 1f;
        indicator.getItemView(1).setMinimumWidth(30);
        indicator.setOnTransitionListener(new OnTransitionTextListener().setColor(selectColor, unSelectColor).setSize(selectSize, unSelectSize));
        indicator.setItemClickable(true);

        indicator.setOnItemSelectListener(new Indicator.OnItemSelectedListener() {
            @Override
            public void onItemSelected(View selectItemView, int select, int preSelect) {
                if (select == 0) {
                    ownerType = 0;
                } else if (select == 1) {
                    ownerType = 1;
                } else if (select == 2) {
                    ownerType = 2;
                } else if (select == 3) {
                    ownerType = 3;
                } else if (select == 4) {
                    ownerType = 4;
                }
                if (stallAdapter != null) {
                    currentPage = 1;
                    getListData(ownerType, orderBy,currentPage,keyWords,"refresh");
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.synth_sort:
                orderBy = 0;
                if (stallAdapter != null) {
                    currentPage = 1;
                    getListData(ownerType, orderBy,currentPage,keyWords,"refresh");
                }
                tv_synth.setTextColor(this.getResources().getColor(R.color.main_color));
                tv_price.setTextColor(this.getResources().getColor(R.color.gray03_color));
                tv_earnings.setTextColor(this.getResources().getColor(R.color.gray03_color));
                break;
            case R.id.price_sort:
                orderBy = 1;
                if (stallAdapter != null) {
                    currentPage = 1;
                    getListData(ownerType, orderBy,currentPage,keyWords,"refresh");
                }
                tv_synth.setTextColor(this.getResources().getColor(R.color.gray03_color));
                tv_price.setTextColor(this.getResources().getColor(R.color.main_color));
                tv_earnings.setTextColor(this.getResources().getColor(R.color.gray03_color));
                break;
            case R.id.earnings_sort:
                orderBy = 2;
                if (stallAdapter != null) {
                    currentPage = 1;
                    getListData(ownerType, orderBy,currentPage,keyWords,"refresh");
                }
                tv_synth.setTextColor(this.getResources().getColor(R.color.gray03_color));
                tv_price.setTextColor(this.getResources().getColor(R.color.gray03_color));
                tv_earnings.setTextColor(this.getResources().getColor(R.color.main_color));
                break;
            case R.id.stall_search:
                startActivity(new Intent(this, SearchStallActivity.class));
                break;
            default:
                break;
        }
    }

    public void getListData(int ownerType, int orderBy,int page, String keyWords, final String handTag) {
        try {
            if (CommonUtil.isNetworkAvailable(this) == 0) {
                CommonUtil.showToask(this, "当前无网络连接，请检查网络设置");
                return;
            }
            mRequest = NoHttp.createStringRequest(Urls.SPACE_IP, LrConstant.POST);
            mRequest.add("action", "GetVitrualParkSpaceList");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("lng", Params.CurrentLon);
            mRequest.add("lat", Params.CurrentLat);
            mRequest.add("pageIndex", page);
            mRequest.add("ownerType", ownerType);//车位类型0-4
            mRequest.add("orderBy", orderBy);//排序1-3
            mRequest.add("keyWords", keyWords);

            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomHttpListener<StallM>(this, true, StallM.class) {
                        @Override
                        public void doWork(StallM data, String code) {
                            if ("1".equals(code)) {
                                if (stallAdapter != null) {
                                    if (data != null && data.getData().size() != 0) {
                                        //no_data.setVisibility(View.GONE);
                                        //lvSearchList.setVisibility(View.VISIBLE);
                                        if (handTag.equals("refresh")) {
                                            no_data.setVisibility(View.GONE);
                                            stallList = data.getData();
                                            stallAdapter.setNewData(data.getData());
                                        } else {
                                            stallList.addAll(data.getData());
                                            stallAdapter.notifyDataSetChanged();
                                        }
                                        stallAdapter.loadMoreComplete();
                                    } else {
                                        if (handTag.equals("refresh")) {
                                            stallList.clear();
                                            stallAdapter.notifyDataSetChanged();
                                            no_data.setVisibility(View.VISIBLE);
                                        } else {
                                            stallAdapter.loadMoreEnd();
                                        }
                                    }
                                }
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if ("0".equals(code)) {
                                    //no_data.setVisibility(View.VISIBLE);
                                    //lvSearchList.setVisibility(View.GONE);
                                    if (stallAdapter != null) {
                                        if (handTag.equals("refresh")) {
                                            stallList.clear();
                                            stallAdapter.notifyDataSetChanged();
                                            no_data.setVisibility(View.VISIBLE);
                                        } else {
                                            stallAdapter.loadMoreEnd();
                                        }
                                    }
                                    if (obj.getString("msg").contains("用户已在其他手机上登录")) {
                                        CarCommon carCommon = new CarCommon();
                                        carCommon.setClickSureListener(new CarCommon.ClickSureListener() {
                                            @Override
                                            public void setSure() {
                                                Intent intent = new Intent(StallActivity.this, VerificaActivity.class);
                                                startActivity(intent);
                                                finish();
                                            }
                                        });
                                        carCommon.CommonDialog(StallActivity.this, "登录信息已过期，是否重新登录？","下线通知");
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    }, false);
        } catch (Exception e) {

        }
    }
}
